package com.hfut.eeg.common.component;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.common.component
 * @Description: TODO
 * @date 2019-11-23 16:28
 */
@Component
public class EEGFileMonitor{

    @Autowired
    private  EEGFileMonitor EEGmonitor;

    @Value("${eeg.parentFile}")
    private String parentFile;

    //创建多个Monitor会不会出现问题？
    @Autowired
    private EEGfileListener listener;

    @Autowired
    private EEGThread thread;

    /**
    * 文件监听，项目启动，这个bean加载完成之前开始执行
    */
    @PostConstruct
    public void  initMonitor(){
        File parentDirectory = FileUtils.getFile(parentFile);

        //文件过滤
        IOFileFilter ioFileFilter = FileFilterUtils.and(
                FileFilterUtils.fileFileFilter(),
                FileFilterUtils.suffixFileFilter(".EEG"));
        FileAlterationObserver observer = new FileAlterationObserver(parentDirectory,ioFileFilter);
        observer.addListener(listener);
        FileAlterationMonitor monitor = new FileAlterationMonitor(2, observer);
        try {
            monitor.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
